<?php
/*
 * This is the core idea.
 * Object Fork splits recursively the application process for the required number of threads (about 30).
 * Every child process gets a unique number stored in binary system (myName). Then the number of the
 * process, after conversion to the decimal system, is used to find the right server with which the
 * specific process will be connected (function doSQL).
 */
class Fork 
{
    public function walkAround($forkArray) 
    {
        if (!isset($forkArray['step'])) {
            $forkArray['step'] = 0;
        }
        if (!isset($forkArray['stopAt'])) {
            $forkArray['stopAt'] = 1;
        }
        if ($forkArray['stopAt'] == 1) {
            while (count($forkArray['depotsAndParcels']) > pow(2, $forkArray['stopAt'])) {
                $forkArray['stopAt']++;
            }    
        }
        if ($forkArray['stopAt'] > 8) { 
            die("ERROR: var stopAt@fork is to big"); 
        }
        if ($forkArray['step'] >= $forkArray['stopAt']) { 
            die("ERROR: var step@fork is to big: " . $forkArray['step'] . '>=' . $forkArray['stopAt']); 
        }
        $forkArray['step']++;

        $pid = pcntl_fork();

        if($pid) {
            $forkArray['myName'] .= '0';
        } else {
            $forkArray['myName'] .= '1';
        }
        if ($forkArray['step'] < $forkArray['stopAt']) { 
            $this->walkAround($forkArray);
        } elseif ($forkArray['step'] == $forkArray['stopAt']) { 
            $forkArray['myName'] = bindec($forkArray['myName']);
            if ($forkArray['depotsAndParcels'][$forkArray['myName']]['depot']) {
                $this->doSQL($forkArray);
            }
        } else { 
            die("ERROR: var step@fork is to big");
        }
    }

    private function doSQL($forkArray) 
    {
        $depot_conn = 'depot@pldpt' . $forkArray['depotsAndParcels'][$forkArray['myName']]['depot'] . '00';
        //         np. depot@pldpt6200
        $conn_id = ifx_connect($depot_conn, "xxxxxx", "xxxxxxx" ) or die ("connect " . $forkArray['depotsAndParcels'][$forkArray['myName']]['depot'] . " error, 1");
        foreach ($forkArray['queriesAndFiles'] as $q) {
            $file    = "/tools/sender/data/get/FORK-"  . $forkArray['depotsAndParcels'][$forkArray['myName']]['depot'] . "-" . $q['file'];
            $fileTmp = "/tools/sender/data/get/xFORK-" . $forkArray['depotsAndParcels'][$forkArray['myName']]['depot'] . "-" . $q['file'];
            //          /tools/sender/data/get/xFORK-62-pcl_prod_consol
            $q = $output = null;
            if (!((is_file($file)) || (is_file($fileTmp)))) {
                touch($fileTmp);
                foreach ($forkArray['depotsAndParcels'][$forkArray['myName']]['parcels'] as $p) { 
                    $q_tmp  = preg_replace('/XXXXXXXXXXX/', $p, $q['query']);
                    $res_id = ifx_query($q_tmp, $conn_id);
                    $row    = ifx_fetch_row ($res_id);
                    if ($row) {
                        while (is_array($row)) {
                            foreach ($row as $col) { 
                                $output .= $col . '|';  
                            }
                            $output .= "\n"; 
                            $row     = ifx_fetch_row ($res_id);
                        }
                    }
                }
            }
            if ($output) {
                file_put_contents($fileTmp, $output);
                rename($fileTmp, $file);
            }
            $q =@ unlink($fileTmp);
        }
    }
}
?>
